<!DOCTYPE html>
<title>WindowClient.navigate() for prerendered main page</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script src="resources/utils.js"></script>
<body>
<script>
const PAGE_URL = 'resources/prerendered-page.html';
const WORKER_URL = 'resources/windowclient-navigate-worker.js';
const SAME_ORIGIN_DESTINATION =
    get_host_info()['HTTPS_ORIGIN'] + base_path() + 'resources/empty.html';

promise_test(async t => {
  const registration =
    await service_worker_unregister_and_register(t, WORKER_URL, PAGE_URL);
  t.add_cleanup(() => registration.unregister());
  await wait_for_state(t, get_newest_worker(registration), 'activated');

  const readyChannel = new BroadcastChannel('prerender-channel');
  t.add_cleanup(_ => readyChannel.close());

  const readyPromise = new Promise(resolve => {
    readyChannel.addEventListener('message', e => {
      resolve(e.data);
    }, {
      once: true
    });
  });

  startPrerendering(PAGE_URL);

  const prerenderResult = await readyPromise;
  assert_equals(prerenderResult, 'prerender success',
                'should succeed to prerender a page');

  const resultChannel = new BroadcastChannel('test-channel');
  t.add_cleanup(_ => resultChannel.close());

  const navigationPromise = new Promise(resolve => {
    resultChannel.addEventListener('message', e => {
      resolve(e.data);
    }, {
      once: true
    });
  });

  // Asks the service worker to navigate the prerendered page to `navigationUrl`
  // via `WindowClient.navigate(url)`.
  registration.active.postMessage({
    navigationUrl: SAME_ORIGIN_DESTINATION,
    clientUrl: new URL(PAGE_URL, window.location).toString(),
    respondTo: 'test-channel',
  });

  const navigationResult = await navigationPromise;
  assert_equals(navigationResult, 'navigate() failed with TypeError',
      'should fail the navigation with TypeError');
}, 'WindowClient.navigate() for a prerendered main page should throw a' +
   'TypeError');
</script>
</body>
